class Solution
{
public:
    long long minimumRemoval(vector<int> &beans)
    {
        map<long long, long long> counts;
        long long n = beans.size();
        long long sumBean = 0;
        for (int bean : beans)
        {
            sumBean += bean;
            ++counts[bean];
        }

        long long curCount = 0;
        long long result = sumBean;
        for (auto &[bean, count] : counts)
        {
            long long backCount = n - curCount;
            long long cost = sumBean - backCount * bean;
            result = min(result, cost);

            curCount += count;
        }

        return result;
    }
};